package com.ztsoft.user.service.dynamic.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.ztsoft.user.mapper.dynamic.DynamicConfigMapper;
import com.ztsoft.user.pojo.entity.dynamic.DynamicConfig;
import com.ztsoft.user.pojo.enums.ResultCode;
import com.ztsoft.user.service.dynamic.DynamicConfigService;
import com.ztsoft.user.utils.Result;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DynamicConfigServiceImpl extends ServiceImpl<DynamicConfigMapper, DynamicConfig> implements DynamicConfigService {
    /**
     * 保存表格配置
     *
     * @param dynamicConfig
     * @return
     */
    @Override
    public Result saveDynamicConfig(DynamicConfig dynamicConfig) {
        Date date = new Date(System.currentTimeMillis());
        List<DynamicConfig> list = this.list(new QueryWrapper<DynamicConfig>()
                .eq("table_code", dynamicConfig.getTableCode())
                .eq("config_type", dynamicConfig.getConfigType()));
        if (list.isEmpty()) {
            dynamicConfig.setCreateTime(date);
            dynamicConfig.setModifyTime(date);
            this.save(dynamicConfig);
            return Result.success();
        } else {
            return Result.returnMsg("添加失败，同一表格下不允许添加同一类型的表单", ResultCode.FAILED.getCode());
        }
    }

    /**
     * 保存表格配置
     */
    @Override
    public Result<Object> saveConfig(String tableCode, Map<String, Map<String, Object>> map) {
        Date date = new Date(System.currentTimeMillis());
        try {
            map.forEach((configType, infoMap) -> {
                DynamicConfig dynamicConfig = new DynamicConfig();
                Boolean enabled = (Boolean) infoMap.get("enabled");
                String contents = infoMap.get("codeConfig").toString();
                String codeConfigForm = infoMap.get("codeConfigForm").toString();

                dynamicConfig.setConfigType(configType);
                dynamicConfig.setTableCode(tableCode);
                dynamicConfig.setContents(contents);
                dynamicConfig.setModifyTime(date);
                dynamicConfig.setStatus(enabled);
                dynamicConfig.setCodeConfigForm(codeConfigForm);

                List<DynamicConfig> list = this.list(new QueryWrapper<DynamicConfig>()
                        .eq("table_code", dynamicConfig.getTableCode())
                        .eq("config_type", dynamicConfig.getConfigType()));
                if (list.isEmpty()) {
                    dynamicConfig.setCreateTime(date);
                    this.save(dynamicConfig);
                } else if (list.size() == 1) {
                    dynamicConfig.setId(list.get(0).getId());
                    this.updateById(dynamicConfig);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
        return Result.success();

    }

    @Override
    public Map<String, Map<String, Object>> queryConfigByTableCode(String tableCode) {
        List<DynamicConfig> configList = baseMapper.selectList(new QueryWrapper<DynamicConfig>().eq("table_code", tableCode));
        Map<String, Map<String, Object>> map = new HashMap<>();
        configList.forEach(dynamicConfig -> {
            String configType = dynamicConfig.getConfigType();
            Map<String, Object> map1 = new HashMap<>();
            Boolean status = dynamicConfig.getStatus();
            String codeConfigForm = dynamicConfig.getCodeConfigForm();

            map1.put("codeConfigForm", codeConfigForm);
            map1.put("enabled", status);
            map.put(configType, map1);
        });

        return map;
    }
}